package com.zeei.ems.meet.service.impl;

import com.zeei.ems.base.util.WaterJudegUtil;
import com.zeei.ems.meet.dao.DataRtDao;
import com.zeei.ems.meet.model.DataRtVO;
import com.zeei.ems.meet.model.TaskDetailVO;
import com.zeei.ems.meet.model.dto.DataRtDto;
import com.zeei.ems.meet.model.vo.DataRtParam;
import com.zeei.ems.meet.service.DataRtService;
import com.zeei.ems.meet.util.BeanMapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DataRtServiceImpl implements DataRtService {
    @Autowired
    DataRtDao dataRtDao;

    @Override
    public List<DataRtDto> listDataRt(DataRtParam dataRtParam) {
        List<DataRtDto> resultList = new ArrayList<>();
        List<DataRtVO> dataRtList = dataRtDao.listDataRt(dataRtParam);
        if (CollectionUtils.isEmpty(dataRtList)) {
            return resultList;
        }
        dataRtList.forEach(dataRt -> {
            dataRt.setQrCode(dataRt.getQrCode().substring(0, dataRt.getQrCode().lastIndexOf("_")));
        });

        // 对站点进行分组
        Map<String, List<DataRtVO>> pointMap = dataRtList.stream().collect(Collectors.groupingBy(DataRtVO::getQrCode));
        // 对因子进行分组
        Map<String, List<DataRtVO>> polluteMap = dataRtList.stream().collect(Collectors.groupingBy(DataRtVO::getPolluteCode));
        for (Map.Entry<String, List<DataRtVO>> qrCode : pointMap.entrySet()) {
            List<DataRtVO> pointDataList = qrCode.getValue();
            if (CollectionUtils.isEmpty(pointDataList)) {
                continue;
            }

            // 一个站点一行数据
            DataRtDto dataRtDto = new DataRtDto();
            dataRtDto.setPointCode(pointDataList.get(0).getPointCode());
            dataRtDto.setPointName(pointDataList.get(0).getPointName());
            dataRtDto.setSampleCode(qrCode.getKey());
            dataRtDto.setSampleName("样品编码");

            // 设置样品编码信息
            List<DataRtVO> dataList = new ArrayList<>();
            for (Map.Entry<String, List<DataRtVO>> polluteCode : polluteMap.entrySet()) {
                List<DataRtVO> dataCollectList = pointDataList.stream().filter(item -> item.getPolluteCode().equals(polluteCode.getKey())).collect(Collectors.toList());
                // 如果站点对应的因子数据存在
                if (!CollectionUtils.isEmpty(dataCollectList)) {
                    dataList.add(dataCollectList.get(0));
                } else {
                    List<DataRtVO> valueList = polluteCode.getValue();
                    if (!CollectionUtils.isEmpty(valueList)) {
                        DataRtVO dataPolluteRt = valueList.get(0);
                        DataRtVO dataRt = new DataRtVO();
                        dataRt.setPolluteCode(polluteCode.getKey());
                        dataRt.setPolluteName(dataPolluteRt.getPolluteName());
                        dataRtDto.setPointCode(dataPolluteRt.getPointCode());
                        dataRtDto.setPointName(dataPolluteRt.getPointName());
                        dataList.add(dataRt);
                    }
                }
            }

            dataRtDto.setDataList(dataList);
            resultList.add(dataRtDto);
        }

        return resultList;
    }

    @Override
    public List<TaskDetailVO> listDataRtTaskDetail(String taskCode) {
        List<TaskDetailVO> taskDetailList = new ArrayList<>();
        DataRtParam dataRtParam = new DataRtParam();
        dataRtParam.setTaskCode(taskCode);
        List<DataRtVO> dataRtList = dataRtDao.listDataRt(dataRtParam);
        if (!CollectionUtils.isEmpty(dataRtList)) {
            taskDetailList = BeanMapperUtils.mapList(dataRtList, TaskDetailVO.class);
            taskDetailList.forEach(taskDetail -> {
                Map<String, Double> map = new HashMap<>();
                map.put(taskDetail.getPolluteCode(), Double.parseDouble(taskDetail.getDataValue()));
                if (taskDetail.getTaskObject() != null && taskDetail.getTaskObject().equals("taskObject-lake")){
                    map.put("secType", null);
                }
                taskDetail.setWaterLevel(WaterJudegUtil.getLevelCode(map));

                if (taskDetail.getDataFlag() != null && "s".equals(taskDetail.getDataFlag().toLowerCase())){
                    taskDetail.setRemark("手动上报");
                }else{
                    taskDetail.setRemark("自动上报");
                }
            });
        }

        return taskDetailList;
    }
}
